﻿using System.Linq;
using System.Collections.Concurrent;
using System.Collections.Generic;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.DataStore;
using NTTDATA.BEENChina.ResultCertification.model;

namespace NTTDATA.BEENChina.ResultCertification.Logic
{
     
    /// <summary>
    /// 单例 得到全部的jaText
    /// </summary>
    public class JaTexts
    {
  
        private object lockobject = new object();


        private JaTexts(){}


        private ConcurrentDictionary<EnumTextType, List<JaTextItem>> _jaTextItems = null;


        public  List<JaTextItem> GetJaTextItems(EnumTextType textType)
        {


            lock (lockobject)
            {
                if (_jaTextItems == null || !_jaTextItems.Keys.Contains(textType))
                {

                    IJaTextClientAccess  clientAccess = new JaTextClientAccess();
                    
                    var items = clientAccess.GetJaText(textType);

                    if (items != null && items.Any())
                    {
                        if (_jaTextItems == null)
                            _jaTextItems = new ConcurrentDictionary<EnumTextType, List<JaTextItem>>();

                        _jaTextItems.TryAdd(textType, items);
                    }

                }

                List<JaTextItem> outItems = null;

                if (_jaTextItems != null) 
                    _jaTextItems.TryGetValue(textType, out outItems);

                return outItems;
            }
         }
         

        private static JaTexts _jaTexts = null;

        public static JaTexts GetInstance()
        {
            if (_jaTexts == null)
                _jaTexts = new JaTexts();

            return _jaTexts;
        }
    }
}
